﻿using UnityEngine;
// 点击拖拽模型 模型上要有碰撞组件 
public class DragMode : MonoBehaviour
{
    private Vector3 screenPoint;
    private Vector3 offset;    
 
    private void OnMouseDown()
    {
        screenPoint = Camera.main.WorldToScreenPoint(transform.position);
        // 计算鼠标的3维坐标跟物体的3维坐标的差值
        offset = transform.position - Camera.main.ScreenToWorldPoint(new 
            Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    }
 
    private void OnMouseDrag()
    {
        //鼠标的平面坐标
        Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 
            screenPoint.z);
        //鼠标转移的3d空间坐标值
        Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
        // 改变鼠标的3D空间坐标
        transform.position = curPosition;
    }
}